<?php

/**
 *
 */
abstract class FileManagerLibrary implements Serializable {

	/**
	 * The Public Name of this Library (is displayed in FIleManager as Root-Folder)
	 * @var string
	 */
	private $name;
	
	/**
	 * Saves the Instance of the FileManager to wich this Library is assigned to
	 * @var FileManager
	 */
	private $fm;
	
	/**
	 * Gets the Folder Contents for the left side, return must be in the format r[0]['Name'], r[0]['type']="Directory" etc.
	 * @param $dir Provides the path starting at the root of the Library, has ending "/"
	 * @return array
	 */
	public abstract function getSubTree($dir);
	
	/**
	 * Gets the Right Content for a Path inside the Library, the return value must 
	 * be an array with elements structured like the return value of getInfo();
	 * @param $path
	 * @return array
	 */
	public abstract function getFolder($path);
	
	/**
	 * Returns an assoziative Array with Informations about the spefified Folder or File:
	 * - Path
	 * - File Type
	 * - File Type Title
	 * - Preview: URL to Preview Image
	 * - Properties["Date Created"|"Date Modified"|"Height"|"Width"|"Size"]
	 * - Error: null if OK
	 * - Code: 0 if OK
	 * 
	 * @param $path
	 * @return array
	 */
	public abstract function getInfo($path);

	/**
	 * Adds a Folder with $name at the fiven path
	 * @param $path - Path to the folder
	 * @param $name - Name of the new Folder
	 * @return true or Name of new Folder if OK, otherwise false
	 */
	public abstract function addFolder($path, $name);
	
	/**
	 * Uploads a File with the given Name to Path
	 * @param $path - Path to the FOlder in wich the file should be stored
	 * @param $file - Name of File
	 * @return true or Name of new File if OK, otherwiese false
	 */
	public abstract function addFile($path, $file);
	
	/**
	 * Sends the requested File to the Browser, you can use the
	 * @see sendFile($filePath, $displayName)-Function to send the file.
	 * @param $path - Path to the File in Library
	 * @return void
	 */
	public abstract function downloadFile($path);
	
	/**
	 * Renames a File or Folder
	 * @param $path - Path to the File or Path
	 * @param $new - New Name without Path
	 * @return true or new Name if OK, otherwiese false
	 */
	public abstract function rename($path, $new);
	
	/**
	 * Deletes a given File or Folder (recursive)
	 * @param $path - Path to File or Folder
	 * @return true on success, otherwise false
	 */
	public abstract function delete($path);
	
	public function setName($s) {
		$this->name = $s;
	}
	
	public function getName() {
		return $this->name;
	}
	
	public function setFileManager($v) {
		$this->fm = $v;
	}
	
	public function getFileManager() {
		return $this->fm;
	}
	
	protected function getIconUrl($ext) {
		if($this->fm) {
			return $this->fm->getIconUrl($ext);
		}
		else {
			return null;
		}
	}
	
	protected function sendFile($filePath, $displayName) {
		
		if(file_exists($filePath)) {
			$info = pathinfo($filePath);
			
			if(!$displayName) {
				$displayName = $info['basename'];
			}
			
			header("Content-type: application/force-download");
			//header('Content-Disposition: inline; filename="' . $_SERVER['DOCUMENT_ROOT'] . $this->get['path'] . '"');
			header("Content-Transfer-Encoding: Binary");
			header("Content-length: ".filesize($filePath));
			header('Content-Type: application/octet-stream');
			header('Content-Disposition: attachment; filename="' . $displayName . '"');
			readfile($filePath);
			die();
		}
		else {
			die("File not found!");
		}	
	}
	
	/* Some Functions to use the Filesystem Paths as Identifier for other things */
	
	/**
	 * Returns the Level of the Given Path rto a Directory or a File
	 * @param $path
	 * @return int
	 */
	protected function getLevel($path) {
		// Create an array ot Folders
		//$info = pathinfo($path);
		//$a = explode("/", $path);
		//print_r($a);
		return substr_count($path, "/") - 1;
	}
	
	protected function getLastFolder($path) {
		
		// Create an array ot Folders
		//$info = pathinfo($path);
		$a = explode("/", $path);
		
		return $a[count($a)-2];
						
	}
	
}

?>